/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.home;

import static com.dataoke.shoppingguide.app773026.bean.Constants.DA_TAO_KE_GOODS_LIST_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.PRODUCT_ID_KEY;
import static com.dataoke.shoppingguide.app773026.bean.Constants.PRODUCT_LIST_URL;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.detail.DataokeProductDetailActivity;
import com.dataoke.shoppingguide.app773026.adapter.DataokeProductListAdapter;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGoodsDetailBean;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGoodsListResBean;
import com.dataoke.shoppingguide.app773026.util.HttpTools;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class ListViewHolder extends RecyclerView.ViewHolder implements
        DataokeProductListAdapter.OnItemClickListener{
    Fragment fragment;
    RecyclerView mRecycler;

    StaggeredGridLayoutManager layoutManager;
    DataokeProductListAdapter mProductListAdapter;
    private int page = 1;
    private boolean replace = true;

    public ListViewHolder(Fragment fragment, @NonNull View itemView) {
        super(itemView);
        this.fragment = fragment;
        mRecycler = itemView.findViewById(R.id.recycler);
    }

    public void setData(List<DataokeGoodsDetailBean> contentBean) {
        mProductListAdapter = new DataokeProductListAdapter(fragment);
        mProductListAdapter.setData(contentBean);
        layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(layoutManager);
        mRecycler.setAdapter(mProductListAdapter);
        mProductListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(int position, DataokeGoodsDetailBean itemBean) {
        Intent intent = new Intent(fragment.getContext(), DataokeProductDetailActivity.class);
        intent.putExtra(PRODUCT_ID_KEY, itemBean.getGoodsId());
        fragment.startActivity(intent);
    }

    public void addData(List<DataokeGoodsDetailBean> contentBean) {
        mProductListAdapter.addData(contentBean);
    }

    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        initData(null);
        refreshLayout.finishLoadMore(0, true, false);
        page++;
    }

    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.getLayout().postDelayed(() -> {
            replace = true;
            page = 1;
            initData(null);
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();//setNoMoreData(false);//恢复上拉状态
        }, 1500);
    }

    protected void refreshData(List<DataokeGoodsDetailBean> items) {
        fragment.getActivity().runOnUiThread(() -> {
            if(replace) {
                setData(items);
            } else {
                mProductListAdapter.addData(items);
            }

            replace = false;

        });
    }

    public void initData(List<DataokeGoodsDetailBean> list) {
        if(list != null && !list.isEmpty()) {
            page++;
            refreshData(list);
            return;
        }

        HttpTools.get(String.format(DA_TAO_KE_GOODS_LIST_URL, page, "9.9"), new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("HaodankuListFragment", String.format(PRODUCT_LIST_URL, page) + e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                DataokeGoodsListResBean productListBean = JSON.parseObject(response.body().string(), DataokeGoodsListResBean.class);
                if(productListBean.getCode() == 0) {
                    refreshData(productListBean.getData().getList());
                }
            }
        });

    }

}